<?php

namespace App\Admin\Controllers\Owner;

use App\Admin\Repositories\Owner;
use App\Models\Building;
use App\Models\House;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

abstract class CommonOwnerController extends AdminController
{
    abstract protected function map(): array;

    abstract protected function customGrid(Grid $grid);

    protected function grid()
    {
        return Grid::make(
            new Owner(['user', 'ownerable']),
            function (Grid $grid) {
                // 检测
                $this->check($grid);
                
                $grid->model()->where($this->map());
                $grid->disableDeleteButton();
                $grid->disableBatchDelete();
                $grid->id->sortable();
                $grid->column('user.name', '业主');
                $grid->column('user.mobile', '业主电话');
                $grid->column('ownerable', '关联房产')->display(function ($value) {
                    if (isset($value['building_id'])) {  // 房间详细名称显示
                        $building = Building::query()->with('house')->find($value['building_id']);
                        return $building->house->name . " - " . $building->name. " - " . $value['name'];
                    }
                    if (isset($value['house_id'])) {
                        $house = House::query()->find($value['house_id']);
                        return $house->name . " - ". $value['name'];
                    }
                    return $value['name'];
                });
                $grid->column('cycle_price', '每期租金（不含水电等费用）')->display(function ($value) {
                   return $value . '/' . \App\Models\Owner::$cycleUnitMap[$this->cycle_unit];
                });
                $grid->closed('合同状态')->display(
                    function ($value) {
                        return $value ? '开启' : '关闭';
                    }
                );
                $grid->started_at('租期开始时间');
                $grid->ended_at('租期结束时间');
                $grid->disableEditButton();
                $grid->disableViewButton();
                $this->customGrid($grid);

                $grid->filter(function (Grid\Filter $filter) {
                    // 更改为 panel 布局
                    $filter->panel();
                    // 注意切换为panel布局方式时需要重新调整表单字段的宽度
                    $filter->equal('id', '合同id')->width(3);
                    $filter->between('started_at', '合同开始时间')->width(3)->date();
                    $filter->between('ended_at', '合同结束时间')->width(3)->date();
                });
            }
        );
    }

    abstract protected function customDetail($show);

    protected function detail($id)
    {
        return Show::make(
            $id,
            new Owner('user'),
            function (Show $show) {
                $show->id;
                $show->name;
                $show->email;
                $show->mobile;
                $show->password;
                $show->gender;
                $show->address;
                $show->status;
                $show->create_admin_id;
                $show->update_admin_id;
                $show->created_at;
                $show->updated_at;
                $this->customDetail($show);
            }
        );
    }

    private function check($grid)
    {
        // 检测用户是否存在
    }
}
